package com.jumi.microservice.service.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jumi.microservice.common.constant.DelFlag;
import com.jumi.microservice.constant.compensation.AfterSaleStatusEnum;
import com.jumi.microservice.constant.refundgoods.RefundGoodsStatusEnum;
import com.jumi.microservice.dto.apiService.OrderGoodsNumNameResponse;
import com.jumi.microservice.dto.apiService.RefundOrderGoodsResponse;
import com.jumi.microservice.dto.serviceDto.RefundOrderGoodsRequest;
import com.jumi.microservice.entity.OrderGoods;
import com.jumi.microservice.entity.UserOrder;
import com.jumi.microservice.enumerate.LogisticsStatusEnum;
import com.jumi.microservice.enumerate.SelfFlagEnum;
import com.jumi.microservice.mapper.OrderGoodsMapper;
import com.jumi.microservice.mapper.UserOrderMapper;
import com.jumi.microservice.service.IOrderGoodsService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangjingdao
 */
@DubboService(interfaceClass = IOrderGoodsService.class)
public class OrderGoodsServiceImpl implements IOrderGoodsService {
    @Resource
    OrderGoodsMapper orderGoodsMapper;
    @Resource
    UserOrderMapper userOrderMapper;

    @Override
    public List<OrderGoodsNumNameResponse> getGoodsNamePrice(List<String> orderGoods) {
        List<OrderGoodsNumNameResponse> orderGoodsNumNameResponses = new ArrayList<>();
        QueryWrapper<UserOrder> userOrderQueryWrapper = new QueryWrapper<>();
        userOrderQueryWrapper.in("order_no", orderGoods);
        userOrderQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        List<UserOrder> userOrders = userOrderMapper.selectList(userOrderQueryWrapper);
        List<Integer> orderId = userOrders.stream().map(UserOrder::getId).collect(Collectors.toList());
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        orderGoodsQueryWrapper.in("order_id", orderId);
        List<OrderGoods> goods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
        for (UserOrder userOrder : userOrders) {
            for (OrderGoods good : goods) {
                if (good.getOrderId().equals(userOrder.getId())) {
                    if (good.getGoodsAmount() > good.getReturnCount()) {
                        OrderGoodsNumNameResponse orderGoodsNumNameResponse = new OrderGoodsNumNameResponse();
                        orderGoodsNumNameResponse.setGoodsName(good.getGoodsName());
                        orderGoodsNumNameResponse.setOrderNo(userOrder.getOrderNo());
                        if (good.getAfterSealStatus().equals(AfterSaleStatusEnum.REFUND.getCode().byteValue()) || good.getAfterSealStatus().equals(AfterSaleStatusEnum.REFUND_GOODS_AGREE.getCode().byteValue())) {
                            orderGoodsNumNameResponse.setNum(new BigDecimal(good.getGoodsAmount()).subtract(new BigDecimal(good.getReturnCount())).intValue());
                        } else {
                            orderGoodsNumNameResponse.setNum(good.getGoodsAmount());
                        }
                        orderGoodsNumNameResponse.setSpuId(good.getSpuId());
                        orderGoodsNumNameResponse.setReturnCount(good.getReturnCount());
                        orderGoodsNumNameResponse.setPrice(good.getGoodsPrice());
                        orderGoodsNumNameResponse.setInvoicePrice(good.getGoodsPrice());
                        orderGoodsNumNameResponses.add(orderGoodsNumNameResponse);
                    }
                }
            }
        }
        return orderGoodsNumNameResponses;
    }

    @Override
    public List<OrderGoodsNumNameResponse> getInvoiceGoods(List<String> orderGoods) {
        List<OrderGoodsNumNameResponse> orderGoodsNumNameResponses = new ArrayList<>();
        QueryWrapper<UserOrder> userOrderQueryWrapper = new QueryWrapper<>();
        userOrderQueryWrapper.in("order_no", orderGoods);
        userOrderQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        List<UserOrder> userOrders = userOrderMapper.selectList(userOrderQueryWrapper);
        List<Integer> orderId = userOrders.stream().map(UserOrder::getId).collect(Collectors.toList());
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        orderGoodsQueryWrapper.in("order_id", orderId);
        List<OrderGoods> goods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
        for (UserOrder userOrder : userOrders) {
            for (OrderGoods good : goods) {
                if (good.getOrderId().equals(userOrder.getId())) {
                    OrderGoodsNumNameResponse orderGoodsNumNameResponse = new OrderGoodsNumNameResponse();
                    orderGoodsNumNameResponse.setGoodsName(good.getGoodsName());
                    orderGoodsNumNameResponse.setOrderNo(userOrder.getOrderNo());
                    orderGoodsNumNameResponse.setNum(good.getGoodsAmount());
                    orderGoodsNumNameResponse.setSpuId(good.getSpuId());
                    orderGoodsNumNameResponse.setReturnCount(good.getReturnCount());
                    orderGoodsNumNameResponse.setPrice(good.getGoodsPrice());
                    orderGoodsNumNameResponses.add(orderGoodsNumNameResponse);
                }
            }
        }
        return orderGoodsNumNameResponses;
    }

    @Override
    public List<RefundOrderGoodsResponse> getRefundOrderGoods(Integer orderId, List<Integer> goodsIds) {
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("order_id", orderId);
        orderGoodsQueryWrapper.in("goods_id", goodsIds);
        orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
        QueryWrapper<UserOrder> userOrderQueryWrapper = new QueryWrapper<>();
        userOrderQueryWrapper.eq("id", orderId);
        userOrderQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        UserOrder userOrder = userOrderMapper.selectOne(userOrderQueryWrapper);
        List<RefundOrderGoodsResponse> refundOrderGoodsResponses = new ArrayList<>();
        orderGoods.forEach(
                order -> {
                    RefundOrderGoodsResponse refundOrderGoodsResponse = new RefundOrderGoodsResponse();
                    refundOrderGoodsResponse.setOrderNo(userOrder.getOrderNo());
                    refundOrderGoodsResponse.setBuyerId(userOrder.getBuyerId());
                    refundOrderGoodsResponse.setPayType(userOrder.getPayType());
                    refundOrderGoodsResponse.setPayNo(userOrder.getPayNo());
                    refundOrderGoodsResponse.setPayTime(userOrder.getPayTime());
                    refundOrderGoodsResponse.setOrderCreateTime(userOrder.getCreateTime());
                    refundOrderGoodsResponse.setThirdPartyPaySn(userOrder.getThirdPartyPaySn());
                    BeanUtils.copyProperties(order, refundOrderGoodsResponse);
                    if (order.getSelfFlag().equals(SelfFlagEnum.YES_SELF.getCode())) {
                        refundOrderGoodsResponse.setSupplierId(order.getRealWarehouse());
                    } else {
                        refundOrderGoodsResponse.setSupplierId(order.getSupplierId());
                    }
                    refundOrderGoodsResponses.add(refundOrderGoodsResponse);
                }
        );
        return refundOrderGoodsResponses;
    }

    @Override
    public Boolean refundUpdateGoods(RefundOrderGoodsRequest request) {
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("order_id", request.getOrderId());
        orderGoodsQueryWrapper.in("goods_id", request.getGoodsId());
        orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
        for (OrderGoods orderGood : orderGoods) {
            orderGood.setAfterSealStatus(AfterSaleStatusEnum.REFUND.getCode().byteValue());
            orderGood.setRefundId(request.getRefundId());
            orderGood.setGoodsStatus(LogisticsStatusEnum.CANCEL.getCode());
            orderGood.setReturnCount(request.getReturnCount());
            orderGood.setReturnPrice(request.getReturnPrice());
            orderGood.setReturnPostage(request.getReturnPostage());
            orderGood.setGoodsStatus(LogisticsStatusEnum.CANCEL.getCode());
            orderGoodsMapper.updateById(orderGood);
        }
        return true;
    }

    @Override
    public Boolean judgeOrderGoodsStatus(Integer orderId) {
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("order_id", orderId);
        orderGoodsQueryWrapper.ne("after_seal_status", AfterSaleStatusEnum.REFUND.getCode());
        orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
        return orderGoods.size() > 0;
    }

    @Override
    public List<OrderGoodsNumNameResponse> redInvoiceOrderList(List<String> orderGoods) {
        List<OrderGoodsNumNameResponse> orderGoodsNumNameResponses = new ArrayList<>();
        QueryWrapper<UserOrder> userOrderQueryWrapper = new QueryWrapper<>();
        userOrderQueryWrapper.in("order_no", orderGoods);
        userOrderQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        List<UserOrder> userOrders = userOrderMapper.selectList(userOrderQueryWrapper);
        List<Integer> orderId = userOrders.stream().map(UserOrder::getId).collect(Collectors.toList());
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        orderGoodsQueryWrapper.in("order_id", orderId);
        List<OrderGoods> goods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
        for (UserOrder userOrder : userOrders) {
            for (OrderGoods good : goods) {
                if (good.getOrderId().equals(userOrder.getId())) {
                    OrderGoodsNumNameResponse orderGoodsNumNameResponse = new OrderGoodsNumNameResponse();
                    orderGoodsNumNameResponse.setGoodsName(good.getGoodsName());
                    orderGoodsNumNameResponse.setOrderNo(userOrder.getOrderNo());
                    orderGoodsNumNameResponse.setNum(good.getGoodsAmount());
                    orderGoodsNumNameResponse.setSpuId(good.getSpuId());
                    orderGoodsNumNameResponse.setReturnCount(good.getReturnCount());
                    orderGoodsNumNameResponse.setPrice(good.getGoodsPrice());
                    orderGoodsNumNameResponse.setInvoicePrice(good.getGoodsPrice());
                    orderGoodsNumNameResponses.add(orderGoodsNumNameResponse);
                }
            }
        }
        return orderGoodsNumNameResponses;
    }

    @Override
    public OrderGoodsNumNameResponse redInvoiceOrder(String orderNo, Integer goodsId) {
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.lambda().eq(OrderGoods::getOrderNo, orderNo).eq(OrderGoods::getGoodsId, goodsId).eq(OrderGoods::getDelFlag, DelFlag.NORMAL);
        OrderGoods goods = orderGoodsMapper.selectOne(orderGoodsQueryWrapper);
        OrderGoodsNumNameResponse orderGoodsNumNameResponse = new OrderGoodsNumNameResponse();
        orderGoodsNumNameResponse.setGoodsName(goods.getGoodsName());
        orderGoodsNumNameResponse.setOrderNo(goods.getOrderNo());
        orderGoodsNumNameResponse.setNum(goods.getGoodsAmount());
        orderGoodsNumNameResponse.setSpuId(goods.getSpuId());
        orderGoodsNumNameResponse.setReturnCount(goods.getReturnCount());
        orderGoodsNumNameResponse.setPrice(goods.getGoodsPrice());
        orderGoodsNumNameResponse.setInvoicePrice(goods.getGoodsPrice());
        return orderGoodsNumNameResponse;
    }
}
